import Vue from 'vue'
import router from '@/router'
import store from '@/store'

/**
 * 获取uuid
 */
export function getUUID () {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
  })
}

/**
 * 是否有权限
 * @param {*} key
 */
export function isAuth (key) {
  return JSON.parse(sessionStorage.getItem('permissions') || '[]').indexOf(key) !== -1 || false
}

/**
 * 树形数据转换
 * @param {*} data
 * @param {*} id
 * @param {*} pid
 */
export function treeDataTranslate (data, id = 'id', pid = 'parentId') {
  var res = []
  var temp = {}
  for (var i = 0; i < data.length; i++) {
    temp[data[i][id]] = data[i]
  }
  for (var k = 0; k < data.length; k++) {
    if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
      if (!temp[data[k][pid]]['children']) {
        temp[data[k][pid]]['children'] = []
      }
      if (!temp[data[k][pid]]['_level']) {
        temp[data[k][pid]]['_level'] = 1
      }
      data[k]['_level'] = temp[data[k][pid]]._level + 1
      temp[data[k][pid]]['children'].push(data[k])
    } else {
      res.push(data[k])
    }
  }
  return res
}

/**
 * 清除登录信息
 */
export function clearLoginInfo () {
  Vue.cookie.delete('token')
  store.commit('resetStore')
  router.options.isAddDynamicMenuRoutes = false
}



export function parse_xml(content) {
  let xml_doc = null
  try {
    xml_doc = (new DOMParser()).parseFromString(content.replace(/[\n\r\s]/g, ''), 'text/xml')
  } catch (e) {
    return false
  }
  let flag = 0

  function build_xml(index, list, element) {
    let t = []
    for (let i = 0; i < flag; i++) {
      t.push('&nbsp;&nbsp;&nbsp;&nbsp;')
    }
    t = t.join('')
    list.push(t + '&lt;<span class="code-key">' + element.nodeName + '</span>&gt;<br/>')
    for (let i = 0; i < element.childNodes.length; i++) {
      const nodeName = element.childNodes[i].nodeName
      if (element.childNodes[i].childNodes.length === 0) {
        const value_txt = ''
        const item = t + '&nbsp;&nbsp;&nbsp;&nbsp;&lt;<span class="code-key">' + nodeName +
          '</span>&gt;' + value_txt + '&lt;/<span class="code-key">' + nodeName + '</span>&gt;<br/>'
        list.push(item)
      } else if ((element.childNodes[i].childNodes.length === 1 && element.childNodes[i].childNodes[0].nodeValue != null)) {
        const value = element.childNodes[i].childNodes[0].nodeValue
        const value_color = !isNaN(Number(value)) ? 'code-number' : 'code-string'
        const value_txt = '<span class="' + value_color + '">' + value + '</span>'
        const item = t + '&nbsp;&nbsp;&nbsp;&nbsp;&lt;<span class="code-key">' + nodeName +
          '</span>&gt;' + value_txt + '&lt;/<span class="code-key">' + nodeName + '</span>&gt;<br/>'
        list.push(item)
      } else {
        flag++
        build_xml(++index, list, element.childNodes[i])
        flag--
      }
    }
    list.push(t + '&lt;/<span class="code-key">' + element.nodeName + '</span>&gt;<BR/>')
  }

  const list = []
  build_xml(0, list, xml_doc.documentElement)

  return list.join('')
}

